package com.lyh.test.controller;

import com.lyh.test.ResultMessage;
import com.lyh.test.entity.Detail;
import com.lyh.test.entity.Wallet;
import com.lyh.test.entity.WalletDetail;
import com.lyh.test.service.DetailService;
import com.lyh.test.service.WalletDetailService;
import com.lyh.test.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/wallet")
public class WalletController {
	@Autowired
	WalletService walletService;

	@Autowired
	DetailService detailService;

	@Autowired
	WalletDetailService walletDetailService;

	/**
	 * 获取全部信息
	 * @return
	 */
	@GetMapping("/list")
	public ResultMessage getList() {
		List<Wallet> walletList = walletService.getList();
		ResultMessage resultMessage = ResultMessage.newSuccessResult();
        resultMessage.setData(walletList);
        return resultMessage;
	}

	/**
	 * 查询用户钱包余额
	 * @param id
	 * @return
	 */
	@GetMapping("/balance/{id}")
	public ResultMessage getBalanceById(@PathVariable(value="id") Integer id) {
		Wallet wallet = walletService.getBalanceById(id);
		ResultMessage resultMessage = ResultMessage.newSuccessResult();
		resultMessage.setData(wallet.getBalance());
		return resultMessage;
	}

	/**
	 * 用户消费100元
	 * @param id
	 * @return
	 */
	@PostMapping("/cost/{id}")
	public ResultMessage costById(@PathVariable(value="id") Integer id) {
		//获取当前用户信息
		Wallet wallet = walletService.getBalanceById(id);
		//用户余额-100
		float balance = wallet.getBalance()- 100;
		//更新当前用户余额
		wallet.setBalance(balance);
		//保存到wallet表
		walletService.saveById(wallet);
		//消费信息保存到detail表
		Detail detail =new Detail();
		detail.setUsername(wallet.getUsername());
		detail.setStatu(0);//状态为0，消费
		detail.setMoney(100);
		detailService.addCost(detail);

		//wid,did保存到wallet_detail表
		WalletDetail walletDetail = new WalletDetail();
		walletDetail.setWid(id);
		walletDetail.setDid(detail.getId());
		walletDetailService.save(walletDetail);

		ResultMessage resultMessage = ResultMessage.newSuccessResult();
		resultMessage.setData(walletDetail);
		return resultMessage;
	}

	/**
	 * 用户退款20元
	 * @param id
	 * @return
	 */
	@PostMapping("/refund/{id}")
	public ResultMessage refundById(@PathVariable(value="id") Integer id) {
		//获取当前用户信息
		Wallet wallet = walletService.getBalanceById(id);
		//用户余额+20
		float balance = wallet.getBalance()+ 20;
		//更新当前用户余额
		wallet.setBalance(balance);
		//保存到wallet表
		walletService.saveById(wallet);
		//消费信息保存到detail表
		Detail detail =new Detail();
		detail.setUsername(wallet.getUsername());
		detail.setStatu(1);//状态为1，退款
		detail.setMoney(20);
		detailService.addCost(detail);

		//wid,did保存到wallet_detail表
		WalletDetail walletDetail = new WalletDetail();
		walletDetail.setWid(id);
		walletDetail.setDid(detail.getId());
		walletDetailService.save(walletDetail);

		ResultMessage resultMessage = ResultMessage.newSuccessResult();
		resultMessage.setData(walletDetail);
		return resultMessage;
	}

	/**
	 * 查询用户钱包金额变动明细
	 */
	@GetMapping("/detail/{id}")
	public ResultMessage detail(@PathVariable(value="id") Integer id) {
		//获取用户钱包id出现列表(次数)
		List<WalletDetail> walletDetailList = walletDetailService.getWid(id);
		for(WalletDetail walletDetail : walletDetailList){
			//获取detail_id查询明细
			//int wid = walletDetail.getDid();

			//获取用户钱包id与wallet_deytail的wid比较，
			List<Detail> detail = walletDetailService.getDetailList(id);
			ResultMessage resultMessage = ResultMessage.newSuccessResult();
			resultMessage.setData(detail);
			return resultMessage;
		}
		return ResultMessage.newSuccessResult();
	}
}
